package com.caow.androidktbase

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.PowerManager
import android.util.Log
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.android.arouter.launcher.ARouter
import com.caow.androidktbase.bean.Student
import com.caow.androidktbase.bean.Teacher
import com.caow.androidktbase.http.ApiServer
import com.caow.androidktbase.router.ROUTER_QR
import com.caow.androidktbase.router.ROUTER_SECOND
import com.caow.baselibrary.datetimepicker.DateTimeConfig
import com.caow.baselibrary.datetimepicker.dialog.CardDatePickerDialog
import com.caow.baselibrary.http.BaseHttpManager
import com.caow.baselibrary.widget.LoadingDialog
import com.lxj.xpopup.XPopup
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import org.koin.android.ext.android.get
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit


class MainActivity : AppCompatActivity() {

    // 测试http
    private val scope by lazy {
        MainScope()
    }

    private val service: ApiServer by lazy { BaseHttpManager.create(ApiServer::class.java) }

    // koin
    private val student: Student = get()
    private val teacher: Teacher = get()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<Button>(R.id.btn).setOnClickListener {
            scope.launch(Dispatchers.IO) {
                service.homepageInfo()
            }
        }

        findViewById<View>(R.id.btn2).setOnClickListener {
//            startActivity(Intent(this, SecondActivity::class.java))
            ARouter.getInstance().build(ROUTER_SECOND)
                .withString("key", "我是携带的参数")
                .navigation()
        }

        findViewById<Button>(R.id.btn3).setOnClickListener {
            LoadingDialog(this).show()
        }

        findViewById<View>(R.id.btn4).setOnClickListener {
            startActivity(Intent(this, ViewPageActivity::class.java))
        }

        findViewById<View>(R.id.btn5).setOnClickListener {
            acquire()
        }
        findViewById<View>(R.id.btn6).setOnClickListener {
            ARouter.getInstance().build(ROUTER_QR)
                .navigation()
        }
        findViewById<View>(R.id.btn7).setOnClickListener {
            showDatePicker()
        }
        findViewById<View>(R.id.btn8).setOnClickListener {
            showDialog()
        }
        findViewById<View>(R.id.btn9).setOnClickListener {
            looper()
        }

        val context: Context = get()
        Log.i("caow", "context == $context")

        Log.i("caow", "student name == ${student.name}")
        Log.i("caow", "teacher name == ${teacher.name}")

//        supportFragmentManager.beginTransaction().add(R.id.frame_layout, MainFragment()).commit()
    }

    private fun showDialog() {
        XPopup.Builder(this)
            .asConfirm("测试", "测试", "取消", "确定", {
                Log.i("caow", "点击了确定")
            }, {
                Log.i("caow", "点击了取消")
            }, false)
            .show()
    }

    private fun showDatePicker() {
        val dialog = CardDatePickerDialog.builder(this)
            .setTitle("日期选择")
            .setDisplayType(ArrayList<Int>().apply {
                add(DateTimeConfig.YEAR)
                add(DateTimeConfig.MONTH)
                add(DateTimeConfig.DAY)
                add(DateTimeConfig.HOUR)
                add(DateTimeConfig.MIN)
            })
            .setBackGroundModel(CardDatePickerDialog.STACK)
//                .setBackGroundModel(if(isDark) R.drawable.shape_bg_dialog_dark else R.drawable.shape_bg_dialog_light)
            .showBackNow(false)
//            .setMaxTime(maxDate)
//            .setPickerLayout(pickerLayout)
//            .setMinTime(minDate)
//            .setDefaultTime(defaultDate)
            .setTouchHideable(true)
            .setChooseDateModel(DateTimeConfig.DATE_LUNAR)
            .setWrapSelectorWheel(false)
//            .setThemeColor(if (model == R.drawable.shape_bg_dialog_custom) Color.parseColor("#FF8000") else 0)
//                .setAssistColor(Color.parseColor("#DDFFFFFF"))
//                .setDividerColor(Color.parseColor("#222222"))
//            .showDateLabel(checkUnitLabel.isChecked)
//            .showFocusDateInfo(checkDateInfo.isChecked)
            .setOnChoose("选择") { time ->

            }
            .setOnCancel("关闭") {
            }.build()
        dialog.show()
    }

    private fun acquire() {
        val pm = getSystemService(Context.POWER_SERVICE) as PowerManager
        val wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "tag:hhhh")
        wakeLock.acquire()
        wakeLock.release()
    }

    override fun onDestroy() {
        scope.cancel()
        super.onDestroy()
    }

    private fun looper() {


        // 创建一个单线程的定时任务调度器
        val executorService = Executors.newSingleThreadScheduledExecutor()

        try {
            val printcount = 20
            for (index in 0 until printcount) {
                // 延迟 i * 500 毫秒后执行任务
                executorService.schedule({
                    Log.i("caow", "循环中 index = $index")
                }, (index * 500).toLong(), TimeUnit.MILLISECONDS);
            }
        } catch (e: Exception) {
            e.printStackTrace();
        } finally {
            // 关闭线程池，防止资源泄漏
            Log.i("caow", "looper 关闭线程池")
            executorService.shutdown()
        }
    }
}